package canglang.scan.erecord.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import canglang.scan.core.base.dao.BaseDAO;
import canglang.scan.core.base.pagination.PaginatedListHelper;
import canglang.scan.core.base.util.DateUtil;
import canglang.scan.core.base.util.StringUtil;
import canglang.scan.erecord.model.ErecordModel;
import canglang.scan.erecord.to.SearchTO;

@SuppressWarnings("unchecked")
public class ErecordDAO extends BaseDAO {
    public Integer searchCount (SearchTO searchTO) {
        Map map = new HashMap();
        String recordName=StringUtil.convertEmpty2Null(searchTO.getRecordName());
        if(recordName!=null)recordName="%"+recordName+"%";
        map.put("recordName", recordName);
        map.put("recordType", StringUtil.convertEmpty2Null(searchTO.getRecordType()));
        String recordYear=StringUtil.convertEmpty2Null(searchTO.getRecordYear());
        if(recordYear!=null){
            map.put("recordYear", Integer.parseInt(recordYear));
        }else{
            map.put("recordYear", 0);
        }
        String refNo=StringUtil.convertEmpty2Null(searchTO.getRefNo());
        if(refNo!=null)refNo="%"+refNo+"%";
        map.put("refNo", refNo);
        String source=StringUtil.convertEmpty2Null(searchTO.getSource());
        if(source!=null)source="%"+source+"%";
        map.put("source", source);
        String appellee=StringUtil.convertEmpty2Null(searchTO.getAppellee());
        if(appellee!=null)appellee="%"+appellee+"%";
        map.put("appellee", appellee);
        String undertaker=StringUtil.convertEmpty2Null(searchTO.getUndertaker());
        if(undertaker!=null)undertaker="%"+undertaker+"%";
        map.put("undertaker", undertaker);
        String caseReason=StringUtil.convertEmpty2Null(searchTO.getCaseReason());
        if(caseReason!=null)caseReason="%"+caseReason+"%";
        map.put("caseReason", caseReason);
        map.put("dateFrom", searchTO.getDateFrom());
        if(searchTO.getDateTo()!=null){
        	Date dateTo=DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1);
            map.put("dateTo",dateTo); 
        }
        return (Integer) getSqlMapClientTemplate().queryForObject("searchErecordCount", map);
    }

    /**
     * call this method to get pagination record list
     * 
     * @param helper
     * @param oldSearchTO
     * @return
     */
    public List searchList (PaginatedListHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        String recordName=StringUtil.convertEmpty2Null(searchTO.getRecordName());
        if(recordName!=null)recordName="%"+recordName+"%";
        map.put("recordName", recordName);
        map.put("recordType", StringUtil.convertEmpty2Null(searchTO.getRecordType()));
        String recordYear=StringUtil.convertEmpty2Null(searchTO.getRecordYear());
        if(recordYear!=null){
            map.put("recordYear", Integer.parseInt(recordYear));
        }else{
            map.put("recordYear", 0);
        }
        String refNo=StringUtil.convertEmpty2Null(searchTO.getRefNo());
        if(refNo!=null)refNo="%"+refNo+"%";
        map.put("refNo", refNo);
        String source=StringUtil.convertEmpty2Null(searchTO.getSource());
        if(source!=null)source="%"+source+"%";
        map.put("source", source);
        String appellee=StringUtil.convertEmpty2Null(searchTO.getAppellee());
        if(appellee!=null)appellee="%"+appellee+"%";
        map.put("appellee", appellee);
        String undertaker=StringUtil.convertEmpty2Null(searchTO.getUndertaker());
        if(undertaker!=null)undertaker="%"+undertaker+"%";
        map.put("undertaker", undertaker);
        String caseReason=StringUtil.convertEmpty2Null(searchTO.getCaseReason());
        if(caseReason!=null)caseReason="%"+caseReason+"%";
        map.put("caseReason", caseReason);
        map.put("dateFrom", searchTO.getDateFrom());
        if(searchTO.getDateTo()!=null){
        	Date dateTo=DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1);
            map.put("dateTo",dateTo);
        }
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchErecordList", map);
    }

    public ErecordModel addErecord (ErecordModel erecordModel) {
        String sql = "addErecord";
        Map map = objectToMap(erecordModel);
        map.put("undertaker", StringUtil.convertEmpty2Null(erecordModel.getUndertaker()));
        map.put("remark", StringUtil.convertEmpty2Null(erecordModel.getRemark()));
        Integer id=(Integer) getSqlMapClientTemplate().insert(sql, map);
        return this.getErecord(id);
    }

    public ErecordModel getErecord (int recordId) {
        String sql = "getErecord";
        Map map = new HashMap();
        map.put("recordId", recordId);
        return (ErecordModel) getSqlMapClientTemplate().queryForObject(sql, map);
    }

    public ErecordModel saveErecord (ErecordModel erecordModel) {
        String sql = "saveErecord";
        Map map = objectToMap(erecordModel);
        map.put("undertaker", StringUtil.convertEmpty2Null(erecordModel.getUndertaker()));
        map.put("remark", StringUtil.convertEmpty2Null(erecordModel.getRemark()));
        getSqlMapClientTemplate().update(sql, map);
        return this.getErecord(erecordModel.getRecordId());
    }

    public void deleteErecord (int recordId) {
        String sql = "deleteErecord";
        Map map = new HashMap();
        map.put("recordId", recordId);
        getSqlMapClientTemplate().delete(sql, map);
    }
}
